var pcid, pcname, eid, ename;

$(function() {
	selectTopCompany();

	$(".selector > input").bind(
			'input',
			function() {
				var inputText = $(this).val();
				$(this).parent().siblings("div").find(">ul>li>a").each(
						function(index, e) {
							if ($(e).html().indexOf(inputText) != -1) {
								$(e).parent().show();
							} else {
								$(e).parent().hide();
							}
						});
			});
});

function selectTopCompany() {
	var url = _COMM_TOP_COMPANY_URL;
	$
			.ajax({
				url : url,
				type : 'get',
				cache : false,
				dataType : 'json',
				success : function(data) {
					var comps = data.result;
					if (!comps) {
						return;
					}
					var $ul = $('<ul></ul>');
					for ( var i in comps) {
						var comp = comps[i];
						var $li = $('<li></li>');
						var $a = $("<a></a>")
						$a.attr('href', '#');
						$a.attr('title', comp.name);
						$a.append(comp.name);
						$a.attr('comp_id', comp.id);
						$li.append($a);
						$ul.append($li);
					}
					$(".compContainer .comp").append($ul);

					$(".compContainer li a")
							.bind(
									'click',
									function(event) {
										event.stopPropagation();

										$(this).parent().css(
												'background-color', '#1c80e5');
										$(this).css('color', '#fff');
										$(this).parent().siblings().css(
												'background-color',
												'transparent');
										$(this).parent().siblings().find('a')
												.css('color', '#000');
										var comp_id = $(this).attr('comp_id');

										pcid = comp_id;
										pcname = $(this).html();

										var nextAll = $(this).parent().parent()
												.parent().parent().nextAll(
														".compContainer");
										var nextAllLength = nextAll.length;
										if (nextAllLength > 0) {
											var i = 0;
											nextAll
													.animate(
															{
																width : 1
															},
															function() {
																nextAll
																		.remove();
																i++;
																if (i == nextAllLength) {
																	$(
																			".compsContainer")
																			.width(
																					($(
																							".compsContainer")
																							.width() - (246 * nextAllLength)));

																	selectSubCompany(comp_id);
																	selectCommunity(comp_id);
																	selectEmployees(
																			comp_id,
																			undefined);
																}
															});
										} else {
											selectSubCompany(comp_id);
											selectCommunity(comp_id);
											selectEmployees(comp_id);
										}
									});

					if (comps.length == 1) {
						$(".compContainer li a").get(0).click();
					}

				}
			});

	function selectSubCompany(compId) {
		var url = _COMM_NODE_COMPANYLIST_URL;
		var p = {};
		p.pid = compId;
		p._ = new Date().getTime();
		$
				.ajax({
					url : url,
					type : 'get',
					cache : false,
					data : p,
					dataType : 'json',
					success : function(data) {
						var comps = data.result;
						if (comps && comps.length > 0) {
							var cont = addSubCompContainer(compId);
							var $ul = $('<ul></ul>');
							for ( var i in comps) {
								var comp = comps[i];
								var $li = $('<li></li>');
								var $a = $("<a></a>")
								$a.attr('href', '#');
								$a.attr('title', comp.name);
								$a.append(comp.name);
								$a.attr('comp_id', comp.id);
								$li.append($a);
								$ul.append($li);
							}
							cont.find(".comp").append($ul);

							$(".compsContainer").width(
									$(".compsContainer").width() + 246);
							cont.css('width', 1);
							$(".compsContainer").append(cont);
							cont.animate({
								width : '240px'
							}, 500);

							scrollContainerBar();

							$("#" + compId + " li a")
									.bind(
											'click',
											function(event) {
												event.stopPropagation();

												$(this).parent().css(
														'background-color',
														'#1c80e5');
												$(this).css('color', '#fff');
												$(this)
														.parent()
														.siblings()
														.css(
																'background-color',
																'transparent');
												$(this).parent().siblings()
														.find('a').css('color',
																'#000');
												var comp_id = $(this).attr(
														'comp_id');

												pcid = comp_id;
												pcname = $(this).html();

												var nextAll = $(this)
														.parent()
														.parent()
														.parent()
														.parent()
														.nextAll(
																".compContainer");
												var nextAllLength = nextAll.length;
												if (nextAllLength > 0) {
													var i = 0;
													nextAll
															.animate(
																	{
																		width : 1
																	},
																	function() {
																		nextAll
																				.remove();
																		i++;
																		if (i == nextAllLength) {
																			$(
																					".compsContainer")
																					.width(
																							($(
																									".compsContainer")
																									.width() - (246 * nextAllLength)));
																			selectSubCompany(comp_id);
																			selectCommunity(comp_id);
																			selectEmployees(
																					comp_id,
																					undefined);
																		}
																	});
												} else {
													selectSubCompany(comp_id);
													selectCommunity(comp_id);
													selectEmployees(comp_id,
															undefined);
												}
											});
						}
					},
					complete : function() {
						hideProccessBar();
					}
				});
	}

	function addSubCompContainer(compId) {
		var $div = $('<div class="compContainer"></div>');
		$div.attr("id", compId);

		var $selectorDiv = $('<div class="selector"></div>');
		var $input = $('<input type="text" placeholder="请输入公司名称"/>');
		$selectorDiv.append($input);

		$input.bind('input', function() {
			var inputText = $(this).val();
			$(this).parent().siblings("div").find(">ul>li>a").each(
					function(index, e) {
						if ($(e).html().indexOf(inputText) != -1) {
							$(e).parent().show();
						} else {
							$(e).parent().hide();
						}
					});
		});

		var $compDiv = $('<div class="comp"></div>');
		$div.append($selectorDiv);
		$div.append($compDiv);

		return $div;
	}

	function scrollContainerBar() {
		var w2 = $(".compsContainer").width();
		var left = $(".compsContainer").scrollLeft();
		$('.container').animate({
			scrollLeft : (left + 150)
		}, 500);
	}

	function selectCommunity(comp_id) {
		var p = {};
		p.pcid = comp_id;
		p._ = new Date().getTime();

		$.ajax({
			url : _SELECT_COMMUNITY_URL,
			type : 'post',
			cache : false,
			data : p,
			dataType : 'json',
			success : function(data) {
				var comms = data.list;
				$(".commContainer .comm ul").remove();
				if (comms && comms.length > 0) {
					var $ul = $('<ul></ul>');
					for ( var i in comms) {
						var comm = comms[i];
						var $li = $('<li></li>');
						var $a = $("<a></a>")
						$a.attr('href', '#');
						$a.append(comm.name);
						$a.attr('comm_id', comm.id);
						$li.append($a);
						$ul.append($li);
					}
					$(".commContainer .comm").append($ul);

					$(".commContainer li a").bind(
							'click',
							function(event) {
								event.stopPropagation();

								$(this).parent().css('background-color',
										'#1c80e5');
								$(this).css('color', '#fff');
								$(this).parent().siblings().css(
										'background-color', 'transparent');
								$(this).parent().siblings().find('a').css(
										'color', '#000');
								var comm_id = $(this).attr('comm_id');

								selectEmployees(undefined, comm_id);
							});
				}
			},
			complete : function() {
				hideProccessBar();
			}
		});
	}

	function selectEmployees(comp_id, comm_id) {
		var p = {};
		p.comp_id = comp_id;
		p.comm_id = comm_id;
		p._ = new Date().getTime();

		$.ajax({
			url : _COMM_EMPLOYEE_LIST_URL,
			type : 'post',
			cache : false,
			data : p,
			dataType : 'json',
			success : function(data) {
				var users = data.result;
				$(".userContainer .user ul").remove();
				if (users && users.length > 0) {
					var $ul = $('<ul></ul>');
					for ( var i in users) {
						var user = users[i];
						var $li = $('<li></li>');
						var $a = $("<a></a>")
						$a.attr('href', '#');
						$a.append(user.name);
						$a.attr("employee_id", user.id);
						$a.attr("employee_name", user.name);
						$a.attr("employee_mobile", user.mobile);
						$a.attr("employee_cid", user.cid);
						$a.attr("employee_cname", user.cName);
						$li.append($a);
						$ul.append($li);
					}
					$(".userContainer .user").append($ul);
					$(".userContainer .user li a").bind(
							'click',
							function(event) {
								event.stopPropagation();

								var eid = $(this).attr('employee_id');
								var emobile = $(this).attr('employee_mobile');
								var ename = $(this).attr('employee_name');
								var cid = $(this).attr('employee_cid');
								var cname = $(this).attr('employee_cname');
								parent.selectedEmployee(pcid, pcname, cid,
										cname, eid, ename, emobile);
								
								if(document.LAYER_INDEX){
									parent.layer.close(document.LAYER_INDEX);
								}else{
									parent.layer.close(parent.layer.index);
								}
							});
				}
			},
			complete : function() {
				hideProccessBar();
			}
		});
	}
}